package com.pan.service.impl;

import com.pan.dao.goodsPriceHistory.GoodsPriceHistoryDao;
import com.pan.dao.goodsPriceHistory.OperationGoodsPriceHistorySelectDao;
import com.pan.entity.OperationGoodsPriceHistory;
import com.pan.entity.Result;
import com.pan.enume.ResultEnum;
import com.pan.excep.error.ServiceException;
import com.pan.service.GoodsPriceHistoryService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.stream.Collectors;


@Service("goodsPriceHistoryService")
public class GoodsPriceHistoryServiceImpl implements GoodsPriceHistoryService {

    @Resource
    private GoodsPriceHistoryDao goodsPriceHistoryDao;
    @Resource
    private OperationGoodsPriceHistorySelectDao goodsPriceHistorySelecteDao;


    @Override
    public Result<List<OperationGoodsPriceHistory>> selectByAdminIdOrGoodsIdOrUpdateDate(String adminTicket, Object selectValue, String selectName){
        if (selectName.equals("updateTime")){
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            try {
                Long startTime = sdf.parse((String) selectValue).getTime();
                Long endTime = startTime + (24*60*60*1000);
                List<OperationGoodsPriceHistory> li =  goodsPriceHistoryDao.select();
                li = li.stream().filter(i->i.getUpdateDate().getTime()>startTime&&i.getUpdateDate().getTime()<endTime?true:false).collect(Collectors.toList());
                return new Result<>(ResultEnum.SUCCESS,li);
            } catch (ParseException e) {
                throw new ServiceException(403,"日期格式错误，应为yyyy-MM-dd");
            }
        }
        if (!selectName.equals("adminId")&&!selectName.equals("goodsId")){
            return new Result<>(ResultEnum.PARAMETER_ERROR);
        }
        List<OperationGoodsPriceHistory> li = null;
        try {
            Field field = OperationGoodsPriceHistory.class.getDeclaredField(selectName);
            String methodName = "selectBy"+selectName.substring(0,1).toUpperCase()+selectName.substring(1);
            Method method = OperationGoodsPriceHistorySelectDao.class.getMethod(methodName,field.getType());
            li = (List<OperationGoodsPriceHistory>) method.invoke(goodsPriceHistorySelecteDao,selectValue);

        } catch (NoSuchMethodException e) {
            throw new RuntimeException(e);
        } catch (InvocationTargetException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        } catch (NoSuchFieldException e) {
            throw new RuntimeException(e);
        }
        if (li==null||li.size()==0){
            return new Result<>(ResultEnum.NOT_FOUND_ERROR);
        }
        Result<List<OperationGoodsPriceHistory>> result =
                new Result<>(ResultEnum.SUCCESS,li);
        return result;

    }

}
